using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.UInt32"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableUInt : INullable
	{
		/// <summary>
		/// The <see cref="System.UInt32"/> value this instance represents.
		/// </summary>
		private uint uintValue;

		/// <summary>
		/// Initializes a new instance of the NullableUInt class.
		/// </summary>
		public NullableUInt()
		{
			this.uintValue = 0;
		}

		/// <summary>
		/// Initializes a new instance of the NullableUInt class with a <see cref="System.UInt32"/> value.
		/// </summary>
		/// <param name="uintValue">The <see cref="System.UInt32"/> that this instance represents.</param>
		public NullableUInt(uint uintValue)
		{
			this.uintValue = uintValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableUInt class with a <see cref="System.String"/> that represents a <see cref="System.UInt32"/>.
		/// </summary>
		/// <param name="uintValue">The string representation of a <see cref="System.UInt32"/> value.</param>
		public NullableUInt(string uintValue)
		{
			this.uintValue = UInt32.Parse(uintValue);
		}

		/// <summary>
		/// Creates a NullableUInt that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableUInt.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableUInt.</param>
		private NullableUInt(SerializationInfo info, StreamingContext context)
		{
			this.uintValue = (uint) info.GetUInt32("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableUInt.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableUInt.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableUInt.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", uintValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.UInt32"/> value this instance represents.
		/// </summary>
		public uint UIntValue
		{
			get { return uintValue; }
			set { uintValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.UInt32"/> to a NullableUInt.
		/// </summary>
		/// <param name="uintValue">The <see cref="System.UInt32"/> to be converted.</param>
		/// <returns>A NullableUInt instance that wraps the <see cref="System.UInt32"/> value.</returns>
		public static implicit operator NullableUInt(uint uintValue)
		{
			return new NullableUInt(uintValue);
		}

		/// <summary>
		/// Checks two NullableUInt instances for equality.
		/// </summary>
		/// <param name="a">A NullableUInt instance to test for equality.</param>
		/// <param name="b">A NullableUInt instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableUInt a, NullableUInt b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.UIntValue == b.UIntValue);
			}
		}

		/// <summary>
		/// Checks two NullableUInt instances for inequality.
		/// </summary>
		/// <param name="a">A NullableUInt instance to test for inequality.</param>
		/// <param name="b">A NullableUInt instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableUInt a, NullableUInt b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.UIntValue != b.UIntValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableUInt)
			{
				NullableUInt nullableUInt = obj as NullableUInt;
				return uintValue.CompareTo(nullableUInt.UIntValue);
			}
			else if (obj is uint)
			{
				return uintValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a uint nor a NullableUInt instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableUInt and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return uintValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableUInt.</returns>
		public override int GetHashCode()
		{
			return uintValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return uintValue.ToString();
		}
	}
}
